/**
 * JSparrow - Une version numérique du jeu "Attention Pirates !" - Copyright (C) 2014 Black Pearl
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If
 * not, see <http://www.gnu.org/licenses/>.
 */
package App.Vue;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import App.Vue.JPanel_Principal;

@SuppressWarnings("serial")
public class Vue_Repaire extends JPanel_Principal{

	private double ratio  = 1;
	private int BORDER = 0;
	final static Point CENTER = new Point(159,101) ;
	//Image à afficher
	private Image repaireImage;
	private Image coffre1;
	private Image coffre2;
	private Image coffre3;
	private Image coffre4;
	private Image coffre5;
	private Image coffre6;
	private Image canon;
	private Image pirate;
	
	private String[] emplacements;
	private int nbtTresors;
	private int nbCaseBas = 1;
	private int nbCaseHaut = 0;

	/**
	 * Créer un nouveau panel de repaire
	 * @param repaire no du repaire (de 0 à 9)
	 * @param emplacements Tableau de String[4] contient la description ex {"pirate",canon","","pirate"}
	 * @param nbTresors nb de trésors restant
	 */
	public Vue_Repaire(int repaire,String[] emplacements,int nbTresors){
		//Le repaire prend 30% de l'écran
		this.setBackground(TRANSPARENT);
		this.setOpaque(false);
		Rectangle screenBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
		this.ratio = 274/(screenBounds.height*0.26);
		int SCRX =(int)(screenBounds.width*0.18); 
		int SCRY = (int)(screenBounds.height*0.26);
		this.setPreferredSize(new Dimension(SCRX,SCRY));
		//Initialisation
		this.emplacements = emplacements;
		this.nbCaseHaut = App.Modele.Configuration.ModelesRepaires.get(repaire).getNbCanonsFixes();
		this.nbCaseBas = App.Modele.Configuration.ModelesRepaires.get(repaire).getNbCanons() + App.Modele.Configuration.ModelesRepaires.get(repaire).getNbPirates();
		this.nbtTresors = nbTresors;
		initImages();

		//Redimensionnement de l'image du repaire
		try{
			BufferedImage bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/repaires/repaire"+ repaire +".png"));
			repaireImage = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
		} catch (IOException e) {
			e.printStackTrace();
		}


	}

	/**
	 * Peint l'arrière plan et les pieces
	 */
	public void paintComponent(Graphics g){
		int i;
		Point p;
		//Dessine le repaire
		g.drawImage(repaireImage, BORDER, BORDER, null);
		Image element;

		//Dessine les pi�ces restantes
		for (i = 0; i < nbCaseBas; i++) {	
			element = getElement(emplacements[i]);
			p = getPosition(i);
			g.drawImage(element, p.x + BORDER, p.y + BORDER, this);
		}
		//Dessine les canons fixes
		for (i = 0; i < nbCaseHaut; i++) {
			p = getPositionFixe(i);
			g.drawImage(canon, p.x + BORDER, p.y + BORDER, this);
		}

		//Dessine les trésors restants
		if(nbtTresors > 0){
			element = getElement("c" + nbtTresors);
			p = new Point((int)(156/ratio), (int)(219/ratio));
			int x = p.x - (int)(element.getWidth(null)/2);
			int y = p.y - (int)(element.getHeight(null)/2);
			g.drawImage(element, x + BORDER, y + BORDER, this);
		}

	}

	/**
	 * Determine la position sur laquelle doit etre placer la piece sur la ligne des dès
	 * @param numEmplacement Numero de l'emplacement
	 * @return Point de la position
	 */
	public Point getPosition(int numEmplacement){
		int x;
		int y = (int)(CENTER.y/ratio);
		int decalage = nbCaseBas%2;
		x = CENTER.x - (int)(nbCaseBas/2)*63 + numEmplacement*63 - (canon.getWidth(null)/2)*decalage;

		return new Point((int)(x/ratio),y);
	}

	/**
	 * Determine la position sur laquelle doit etre placer l'image des canons fixes
	 * @param numEmplacement Numero de l'emplacement
	 * @return Point de la position
	 */
	public Point getPositionFixe(int numEmplacement){
		int x;
		int y = (int)(25/ratio);
		int decalage = nbCaseHaut%2;
		x = CENTER.x - (int)(nbCaseHaut/2)*60 + numEmplacement*63 - (canon.getWidth(null)/2)*decalage;

		return new Point((int)(x/ratio),y);
	}

	/**
	 * Initialise les images et les redimensionne
	 */
	public void initImages(){
		try {
			// Resize les images en fct d'un ratio, permet le faire qu'une fois pour le panel
			BufferedImage bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/coffres/coffre1.png"));
			coffre1 = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/coffres/coffre2.png"));
			coffre2 = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/coffres/coffre3.png"));
			coffre3 = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/coffres/coffre4.png"));
			coffre4 = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/coffres/coffre5.png"));
			coffre5 = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/coffres/coffre6.png"));
			coffre6 = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/canon2.png"));
			canon = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/corsair.png"));
			pirate = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Retourne l'image correspondante à la description
	 * @param type Type de la piece
	 * @return Image de la piece
	 */
	public Image getElement(String type){
		switch(type){
		case "1" :
			return pirate;
		case "2" :
			return canon;
		case "c1" :
			return coffre1;
		case "c2" :
			return coffre2;
		case "c3" :
			return coffre3;
		case "c4" :
			return coffre4;
		case "c5" :
			return coffre5;
		case "c6" :
			return coffre6;
		default : 
			return null;
		}
	}

	/**
	 * Met à jour le panel du repaire
	 * @param repaire no du repaire (de 0 à 9)
	 * @param emplacements Tableau de String[4] contient la description ex {"pirate",coffre","","pirate"}
	 * @param nbTresors nb de trésors restant
	 */
	public void updateRepaire(int repaire,String[] emplacements,int nbTresors){
		this.emplacements = emplacements;
		this.nbCaseBas = App.Modele.Configuration.ModelesRepaires.get(repaire).getNbCanons() + App.Modele.Configuration.ModelesRepaires.get(repaire).getNbPirates();
		this.nbCaseHaut = App.Modele.Configuration.ModelesRepaires.get(repaire).getNbCanonsFixes();
		//TODO: nbCaseHaut
		this.nbtTresors = nbTresors;
		try{
			BufferedImage bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/repaires/repaire"+ repaire +".png"));
			repaireImage = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
		} catch (IOException e) {
			e.printStackTrace();
		}
		repaint();
	}

}


